﻿using System;
using System.Windows;
using DevEnverModal;
using QuailDashboard.View.Data;
using QuailDashboard.View.Models;
using QuailDashboard.View.QuailServiceReference;

namespace QuailDashboard.View.Controls
{
    /// <summary>
    /// The testnote editor class
    /// </summary>
    public partial class TestNoteControl
    {
        private readonly TestNotePageState _state = new TestNotePageState();

        /// <summary>
        /// Initializes a new instance of the <see cref="TestNoteControl"/> class.
        /// </summary>
        public TestNoteControl()
        {
            InitializeComponent();

            // RichTextBoxNotesToolbar.RichTextBox = RichTextBoxNotes;

            DataContext = _state;
        }

        /// <summary>
        /// Fired when the Save button is clicked
        /// </summary>
        public event EventHandler Saved;

        /// <summary>
        /// Gets or sets the ModalControl manager for this instance
        /// </summary>
        public ModalControl Modal
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets the TestNoteId to display
        /// </summary>
        public TestNote TestNote
        {
            get
            {
                return _state.Data;
            }

            set
            {
                _state.Data = value;
            }
        }

        /// <summary>
        /// Gets or sets the current Test
        /// </summary>
        public RunStatusResultWrapper Test
        {
            get; set;
        }

        /// <summary>
        /// Creates a new testnote for the given environment and test
        /// </summary>
        /// <param name="testid">the test id</param>
        /// <param name="creator">the testnote creator</param>
        public void NewTestNote(int testid, string creator)
        {
            TestNote = new TestNote
                           {
                               Active = true,
                               TestId = testid,
                               CreatedBy = creator,
                               CreateDate = DateTime.Now
                           };
        }

        private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            /*
            var gt = RichTextBoxNotes.TransformToVisual(EditGrid);
            var offset = gt.Transform(new Point(0, 0));
            var h = EditGrid.ActualHeight - offset.Y - RowSave.ActualHeight;

            if (h > 0 && h != RichTextBoxNotes.Height)
                RichTextBoxNotes.Height = h;
             * */
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            if (Saved != null)
                Saved(this, EventArgs.Empty);
            Modal.HideModal();
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            Modal.HideModal();
        }

        /// <summary>
        /// Page state for the TestNote control
        /// </summary>
        public class TestNotePageState : Model
        {
            private int _isloading;
            private TestNote _data;

            /// <summary>
            /// Gets the modal window title
            /// </summary>
            public string WindowTitle
            {
                get
                {
                    if (_data == null || _data.TestNoteId == 0)
                        return "Add New Note";

                    return "Edit Note";
                }
            }

            /// <summary>
            /// Gets or sets a value indicating whether the page Is Loading.
            /// </summary>
            public bool IsLoading
            {
                get
                {
                    return _isloading > 0;
                }

                set
                {
                    var old = IsLoading;

                    if (value)
                        _isloading++;
                    else
                        _isloading--;

                    if (old != IsLoading)
                        NotifyPropertyChanged("IsLoading");
                }
            }

            /// <summary>
            /// Gets a value indicating whether the data is new
            /// </summary>
            public bool IsNew
            {
                get
                {
                    return _data != null && _data.TestNoteId == 0;
                }
            }

            /// <summary>
            /// Gets or sets the testnote for the page
            /// </summary>
            public TestNote Data
            {
                get
                {
                    return _data;
                }

                set
                {
                    _data = value;
                    NotifyPropertyChanged("Data");
                    NotifyPropertyChanged("IsNew");
                    NotifyPropertyChanged("WindowTitle");
                }
            }
        }
    }
}
